<template>
  <el-container class="home">
    <!-- 头部区域 -->
    <el-header>
      <figure>
        <img src="../assets/image/logo.png" alt="图标" />
        <figcaption>电商后台管理系统</figcaption>
      </figure>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 内容区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <!-- 侧边栏菜单 -->
      <el-aside :width="isCollapse ? '64px': '200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <el-menu
          :default-active="activePath"
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409EFF"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
        >
          <!-- 一级菜单 -->
          <el-submenu v-for="(item) in menu" :key="item.id" :index="item.id">
            <!-- 一级菜单的模板区 -->
            <template slot="title">
              <!-- 图标 -->
              <i class="iconfont" :class="item.icon"></i>
              <!-- 文本 -->
              <span>{{item.txt}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              v-for="(value) in item.chiledren"
              :key="value.id"
              :index="value.path"
              @click="saveNavState(value)"
            >
              <template slot="title">
                <!-- 图标 -->
                <i class="iconfont" :class="value.icon"></i>
                <!-- 文本 -->
                <span>{{value.txt}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧的主体内容了 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
import menu from "../../data/home-menu.json";
export default {
  name: "home",
  created() {
    this.menu = menu;
    // 获取激活的地址了吧
    this.activePath = window.sessionStorage.getItem("activePath");
  },
  data() {
    return {
      menu: [],
      // 菜单是否折叠
      isCollapse: false,
      // 用户默认的激活链接
      activePath: ""
    };
  },
  methods: {
    // 退出登录功能
    logout() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    // 点击按钮让菜单折叠与展开
    toggleCollapse() {
      this.isCollapse = !this.isCollapse;
    },
    // 保存用户链接的激活状态了
    saveNavState(value) {
      window.sessionStorage.setItem("activePath", value.path);
      this.activePath = value.path;
    }
  }
};
</script>
<style lang="less" scoped>
.home {
  height: 100%;
}
.el-header {
  -webkit-user-select: none;
  user-select: none;
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 10px;
  figure {
    display: flex;
    align-items: center;
    > img {
      width: 35px;
      height: 35px;
      margin-right: 20px;
    }
    > figcaption {
      color: #ffffff;
      font-size: 1.3rem;
      letter-spacing: 4px;
    }
  }
}
.el-aside {
  background-color: #333744;
  .iconfont {
    margin-right: 5px;
  }
  .el-menu {
    -webkit-user-select: none;
    user-select: none;
    border-right: none;
  }
  // 折叠的类名
  .toggle-button {
    height: 30px;
    line-height: 30px;
    background-color: #4a5064;
    font-size: 20px;
    color: #ffffff;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;
  }
}
.el-main {
  background-color: #eaedf1;
}
</style>